package com.youease.chris.view.mediator
{
	import com.youease.chris.control.CreateProjectCommand;
	import com.youease.chris.control.OpenProjectCommand;
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.utils.Common;
	import com.youease.chris.view.OpenProjectTitleWindow;
	import com.youease.chris.manager.TexturesManager;
	import com.youease.chris.manager.ImageLoader;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class OpenProjectMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "OpenProjectMediator";
		public static var INDEX:int = 0;
		private var file:File;
		private var loader:URLLoader;
		private var projectContent:String;
		private var textureContent:String;
		
		public function OpenProjectMediator(viewComponent:Object)
		{
			super(NAME+INDEX, viewComponent);
			openProject.open.addEventListener(MouseEvent.CLICK,onOpenProject);
			openProject.select.addEventListener(MouseEvent.CLICK,onView);
			INDEX++;
		}
		
		public function onView(evt:MouseEvent):void {
			var ff:FileFilter = new FileFilter("proj","*.mapProj");
			file = new File();
			file.addEventListener(Event.SELECT,selectHandler);
			file.browse([ff]);
		}
		
		public function selectHandler(event:Event):void {
			openProject.projectDir.text = file.nativePath;
			file.removeEventListener(Event.SELECT,selectHandler);
			file.addEventListener(Event.COMPLETE,onLoadComplete);
		}
		
		public function onLoadComplete(event:Event):void {
			file.removeEventListener(Event.COMPLETE,onLoadComplete);
			projectContent = file.data.readUTFBytes(file.data.bytesAvailable);
			
			loadTextures();
//			sendNotification(OpenProjectCommand.OPEN_PROJECT,content);
		}
		
		public function loadTextures():void {
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE,onTextureLoadComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			
			var urlRequest:URLRequest = new URLRequest(file.nativePath.substr(0,file.nativePath.length-7)+"textures");
			loader.load(urlRequest);
//			file = new File(file.nativePath.substr(0,file.nativePath.length-7)+"textures");
//			file.addEventListener(Event.COMPLETE,onTextureLoadComplete);
//			file.load();
		}
		
		private function onError(evt:Event):void {
			trace("加载素材出错！！！"+evt.toString());
		}
		
		private function onTextureLoadComplete(event:Event):void {
			loader.removeEventListener(Event.COMPLETE,onTextureLoadComplete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			
			textureContent = loader.data;
			
			sendNotification(OpenProjectCommand.OPEN_PROJECT,[textureContent,projectContent]);
		}
		
		public function onOpenProject(evt:MouseEvent):void {
			
			if(Common.trim(openProject.projectDir.text)=="") {
				trace("工程地址不能为空");
				openProject.dirError.visible = true;
				return;
			}else {
				openProject.dirError.visible = false;
			}
			
			clearPriorProject();
			
			GlobalObject.projectDir = file.parent.nativePath;
			file.load();
		}
		
		private function clearPriorProject():void {
			TexturesManager.clear();
			ImageLoader.instance.clear();
		}
		
		override public function listNotificationInterests():Array {
			return [
				OpenProjectCommand.OPEN_PROJECT_COMPLETE
			];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
				case OpenProjectCommand.OPEN_PROJECT_COMPLETE:
					openProject.closeWindow();
					break;
			}
		}
		
		public function get openProject():OpenProjectTitleWindow {
			return this.viewComponent as OpenProjectTitleWindow;
		}
	}
}